package org.act.BPEL2Automata.bpel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;

import org.act.xservices.bpel11.ProcessFactory;
import org.act.xservices.bpel11.TActivityContainer;
import org.act.xservices.bpel11.TCompensationHandler;
import org.act.xservices.bpel11.TCorrelationSets;
import org.act.xservices.bpel11.TEventHandlers;
import org.act.xservices.bpel11.TFaultHandlers;
import org.act.xservices.bpel11.TPartnerLinks;
import org.act.xservices.bpel11.TProcess;
import org.act.xservices.bpel11.TVariables;
import org.act.xservices.bpel11.util.ProcessXMLProcessor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;


/**
 * 
 * Parse a *.bpel file to get an BPEL object.
 * Methods <b>getVariables()</b> and <b>getActivityContainer()</b>
 * are mostly used.
 * <p>
 * Usage:<p>
 * BPELParser bp = new BPELParser("./files4test/complexType.bpel");<p>
 * bp.parse();<p>
 * TActivityContainer activityContainer = bp.getActivityContainer();<p>
 * ...
 * @author baoxiwu<bb@feijiao.info>,ACT,Beihang University
 * @version 0.9.0
 * @since 2010-03-31
 * 
 */
public class BPELParser {
	private TPartnerLinks partnerLinks = null;
	private TVariables variables = null;
	private TCorrelationSets correlationSets = null;
	private TEventHandlers eventHandlers = null;
	private TFaultHandlers faultHandlers = null;
	private TCompensationHandler compensationHandler = null;
	private TActivityContainer activityContainer = null;
	//private TProcess tProcess = null;
	
	private File file = null;
	
	
	public BPELParser(String filePath){
		this.file = new File(filePath);
	}
	
	public BPELParser(File file){
		this.file = file;
	}
	
	
	//getter methods.
	public TPartnerLinks getPartnerLinks(){
		if(this.partnerLinks != null){
			return this.partnerLinks;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.partnerLinks is null,execute method : parse() first.");
			return null;
		}
	}
	
	public TVariables getVariables(){
		if(this.variables != null){
			return this.variables;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.variables is null,execute method : parse() first.");
			return null;
		}
	}
	
	public TCorrelationSets getCorrelationSets(){
		if(this.correlationSets != null){
			return this.correlationSets;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.correlationSets is null,execute method : parse() first.");
			return null;
		}
	}
	
	public TEventHandlers getEventHandlers(){
		if(this.eventHandlers != null){
			return this.eventHandlers;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.eventHandlers is null,execute method : parse() first.");
			return null;
		}
	}
	
	public TFaultHandlers getFaultHandlers(){
		if(this.faultHandlers != null){
			return this.faultHandlers;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.faultHandlers is null,execute method : parse() first.");
			return null;
		}
	}
	
	public TCompensationHandler getCompensationHandler(){
		if(this.compensationHandler != null){
			return this.compensationHandler;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.compensationHandler is null,execute method : parse() first.");
			return null;
		}
	}
	
	public TActivityContainer getActivityContainer(){
		if(this.activityContainer != null){
			return this.activityContainer;
		}
		else{
			//TODO REPLACE this statement with an common 
			//error or exception handler.
			System.err.println("this.activityContainer is null,execute method : parse() first.");
			return null;
		}
	}
	
	
	public void parse(){
		try {
			ProcessXMLProcessor processor = new ProcessXMLProcessor();
			FileInputStream inputStream = new FileInputStream(this.file.toString());
			Resource resource = processor.load(inputStream,Collections.EMPTY_MAP);

			EObject documentRoot = (EObject) resource.getContents().get(0);
			TProcess process = (TProcess) documentRoot.eContents().get(0);
			
			//System.out.println(process);
			
			this.partnerLinks = process.getPartnerLinks();
			this.variables = process.getVariables();
			this.correlationSets = process.getCorrelationSets();
			this.eventHandlers = process.getEventHandlers();
			this.faultHandlers = process.getFaultHandlers();
			this.compensationHandler = process.getCompensationHandler();
			
			this.activityContainer = ProcessFactory.eINSTANCE.createTActivityContainer();
			
			if(process.getEmpty() != null){
				this.activityContainer.setEmpty(process.getEmpty());
			}
			if(process.getInvoke() != null){
				this.activityContainer.setInvoke(process.getInvoke());
			}
			if(process.getReceive() != null){
				this.activityContainer.setReceive(process.getReceive());
			}
			if(process.getReply() != null){
				this.activityContainer.setReply(process.getReply());
			}
			if(process.getAssign() != null){
				this.activityContainer.setAssign(process.getAssign());
			}
			if(process.getWait() != null){
				this.activityContainer.setWait(process.getWait());
			}
			if(process.getThrow() != null){
				this.activityContainer.setThrow(process.getThrow());
			}
			if(process.getTerminate() != null){
				this.activityContainer.setTerminate(process.getTerminate());
			}
			if(process.getFlow() != null){
				this.activityContainer.setFlow(process.getFlow());
			}
			if(process.getSwitch() != null){
				this.activityContainer.setSwitch(process.getSwitch());
			}
			if(process.getWhile() != null){
				this.activityContainer.setWhile(process.getWhile());
			}
			if(process.getSequence() != null){
				this.activityContainer.setSequence(process.getSequence());
			}
			if(process.getPick() != null){
				this.activityContainer.setPick(process.getPick());
			}
			if(process.getScope() != null){
				this.activityContainer.setScope(process.getScope());
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
